<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>考勤管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <div class="area">
        <div class="input-area">
          <!-- <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="getSignsList"
          >
            <el-button
              type="primary"
              slot="append"
              icon="el-icon-search"
              @click="getSignsList"
            ></el-button>
          </el-input> -->
          <el-button type="primary" @click="dialogVisible = true"
            >添加签到任务
          </el-button>
        </div>
        <!-- <el-col :span="3">
          <el-button type="primary" @click="deleteSigns"
            >删除选中任务</el-button
          >
        </el-col> -->
      </div>
      <!-- 表格 -->
      <el-table
        :data="signList"
        height="450"
        stripe
        border
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" width="55"> </el-table-column>
        <el-table-column
          prop="signTitle"
          width="400"
          label="名称"
        ></el-table-column>
        <el-table-column prop="signDetails" label="内容"> </el-table-column>
        <el-table-column width="110" label="签到码">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-s-grid"
              size="mini"
              @click="creatQrCode(scope.row.signId)"
              >二维码</el-button
            >
          </template>
        </el-table-column>
        <!-- <el-table-column prop="questionType" width="150" label="时间">
          <template slot-scope="scope"> </template>
        </el-table-column> -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- <el-button
              type="primary"
              icon="el-icon-s-grid"
              size="mini" 
              @click="creatQrCode(scope.row.signId)"
              >二维码</el-button
            > -->
            <el-button
              type="primary"
              icon="el-icon-view"
              size="mini"
              @click="gotoCheckUser(scope.row)"
              >查看</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-delete"
              size="mini"
              @click="deleteSign(scope.row.signId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加任务的对话框 -->
    <el-dialog
      title="添加任务"
      :visible.sync="dialogVisible"
      width="50%"
      @close="closeAddForm"
    >
      <el-form
        :model="addForm"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="任务名称" prop="">
          <el-input v-model="addForm.signTitle"></el-input>
        </el-form-item>
        <el-form-item label="任务内容" prop="">
          <el-input v-model="addForm.signDetails"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeAddQuestions">取 消</el-button>
        <el-button @click="addQuestions">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改任务对话框 -->
    <el-dialog
      title="修改任务"
      :visible.sync="editVisible"
      @close="closeEditForm"
    >
      <el-form
        :model="editForm"
        ref="editForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="任务名称" prop="">
          <el-input v-model="editForm.signTitle"></el-input>
        </el-form-item>
        <el-form-item label="任务内容" prop="">
          <el-input v-model="editForm.signDetails"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeEditQuestion">取 消</el-button>
        <el-button @click="editSign">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 展示签到二维码对话框 -->
    <el-dialog
      title="签到二维码"
      :visible.sync="qiandaoVisible"
      @close="closeQiandaoVisible"
    >
      <div class="qrcodeArea">
        <div class="qrcode" id="qrcode" ref="qrCodeUrl"></div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import QRCode from "qrcodejs2";
export default {
  data() {
    return {
      // 打开签到二维码对话框
      qiandaoVisible: false,
      // 二维码url
      textUrl: "",
      // 获取用户列表对象
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 15
      },
      // 任务列表
      signList: [],
      // 添加任务的表单数据
      addForm: {
        signDetails: "",
        signTitle: ""
      },
      // 修改任务的表单数据
      editForm: {},
      // 用户修改信息列表
      userEditList: [],
      total: 0,
      dialogVisible: false,
      editVisible: false, //修改用户信息对话框显示与隐藏
      changeForm: {},
      changeVisible: false,
      // 用户信息
      userinfo: {},
      selectRole: "",
      roleList: [],
      // 用户删除列表
      signDeleteIdList: [],
      // 政治面貌选择
      options: [
        {
          value: 1,
          label: "单选题"
        },
        {
          value: 2,
          label: "多选题"
        }
      ],
      // 当前选中
      value: "",
      // 正确答案单选框
      radio: "",
      // 正确答案多选框
      checkList: []
    };
  },
  created() {
    this.getSignsList();
  },
  methods: {
    // 生成签到二维码
    async creatQrCode(id) {
      this.qiandaoVisible = true;
      let that = this;
      // 获取签到url
      const { data: res } = await this.$http.get("sign/getSignCode/" + id);
      console.log(res);
      if (res.code !== 200) {
        this.qiandaoVisible = false;
        return this.$message.error("获取签到二维码失败！");
      }
      this.textUrl = res.data;
      setTimeout(() => {
        let qrcode = new QRCode(that.$refs.qrCodeUrl, {
          text: this.textUrl, // 需要转换为二维码的内容
          width: 500,
          height: 500,
          colorDark: "#000000",
          colorLight: "#ffffff",
          correctLevel: QRCode.CorrectLevel.H
        });
      }, 0);
    },
    // 关闭签到二维码对话框时将上次生成的二维码清空
    closeQiandaoVisible() {
      document.getElementById("qrcode").innerHTML = "";
    },
    // 多选勾选时将对应questionId添加到删除序列
    handleSelectionChange(rows) {
      this.signDeleteIdList = [];
      rows.forEach(item => {
        this.signDeleteIdList.push(item.signId);
      });
      console.log(this.signDeleteIdList);
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getSignsList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getSignsList();
    },
    // 获取任务列表
    async getSignsList() {
      const { data: res } = await this.$http.get(
        "sign/getAllSign/" + this.queryInfo.pagenum
      );
      console.log(res);
      if (res.code != 200) {
        return this.$message.error("获取签到列表失败！");
      }
      // 用户列表
      this.signList = res.data.signVoList;
      // this.signList = res.data.users;
      // 总数
      // this.total = res.data.total;
      this.total = 11;
    },
    // 关闭对话框清空表单
    closeAddForm() {
      // this.getSignsList();
      this.$refs.ruleForm.resetFields();
    },
    // 关闭编辑信息对话框
    closeEditForm() {
      this.getSignsList();
      this.$refs.editForm.resetFields();
    },
    // 关闭添加任务对话框
    closeAddQuestions() {
      this.dialogVisible = false;
      this.addForm = {
        signDetails: "",
        signTitle: ""
      };
    },
    // 添加任务
    async addQuestions() {
      this.$refs.ruleForm.validate(async value => {
        if (!value) return;
        // 网络请求添加任务
        // 添加任务
        const { data: res } = await this.$http.post(
          "/sign/releaseSign",
          this.addForm
        );
        console.log(res);
        if (res.code !== 200) return this.$message.error("添加签到任务失败！");
        this.$message.success("添加任务成功！");
        this.dialogVisible = false;
        this.addForm = {
          signDetails: "",
          signTitle: ""
        };
        this.getSignsList();
      });
    },
    // 打开修改用户信息对话框
    async showEditDialog(signInfo) {
      // console.log(signInfo);
      this.editForm = signInfo;
      this.editVisible = true;
    },
    // 修改任务
    editSign() {
      this.$refs.editForm.validate(async value => {
        if (!value) return;
        // 网络请求修改任务
        const { data: res } = await this.$http.post(
          "/sign/modifySign",
          this.editForm
        );
        if (res.code !== 200) return this.$message.error("修改签到任务失败！");
        this.$message.success("修改签到任务成功！");
        this.editVisible = false;
        this.getSignsList();
      });
    },
    // 关闭修改任务对话框
    closeEditQuestion() {
      this.getSignsList();
      this.editVisible = false;
    },
    // 删除单个签到任务
    async deleteSign(id) {
      const deleteResult = await this.$confirm("是否删除该签到任务?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(err => err);
      if (deleteResult === "cancel") return this.$message.info("取消删除！");
      const { data: res } = await this.$http.delete("sign/deleteSign/" + id);
      console.log(res);
      if (res.code !== 200) return this.$message.error("删除失败！");
      this.$message.success("删除成功！");
      this.getSignsList();
    },
    // 删除多个任务
    async deleteSigns() {
      const deleteResult = await this.$confirm(
        "是否删除选定签到任务?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (deleteResult === "cancel") return this.$message.info("取消删除！");
      // const { data: res } = await this.$http.delete("users/" + id);
      // if (res.meta.status !== 200) return this.$message.error("删除失败！");
      this.$message.success("删除成功！");
      this.getSignsList();
    },
    // 跳转到签到详情页
    gotoCheckUser(row) {
      window.sessionStorage.setItem("signId", row.signId);
      window.sessionStorage.setItem("signTitle", row.signTitle);
      this.$router.push("/check/checkuser");
    }
  }
};
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 20px;
}
.el-pagination {
  margin-top: 20px;
}
// 二维码区域
.qrcodeArea {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .qrcode {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 20px;
    width: 500px;
    height: 500px;
    padding: 6px;
    box-sizing: border-box;
  }
}

.input-area {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 46%;
  .el-input {
    margin-right: 10px;
  }
}
.select-area {
  width: 10%;
}
.area {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>